﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OBUCare.Core.Models;
using OBUCare.Core.Common;
using OBUCare.Core.DataAccess;


namespace OBUCare.Core.BL
{
    public class UserMgr:IDisposable
    {
        ISSUE_RPTContext db = new ISSUE_RPTContext();

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        public OPERATOR Login(string uid,string pwd,string clientIP)
        {
            var oper = db.GetOPERATOR(uid, pwd);
            if (oper != null)
            {
                //clientIP = "10.11.13.194";//测试
                //clientIP = clientIP == "" ? "10.11.26.13" : clientIP;
                oper.CLIENT_IP = clientIP;

                AgentInfo agent = db.GetAgent(clientIP);

                if (agent != null)
                { 
                    oper.PosID = agent.ID;
                    oper.AgentNo = agent.AgentNo;
                    oper.AgentName = agent.AgentName;
                }
                else
                {
                    //todo:仅用于测试
                    oper.PosID = 25;
                    oper.AgentNo = "110800010016";
                    oper.AgentName = "草桥中心";
                    //oper = null;
                }            
            }

            return oper;
        }

        public string CheckObuNo(string obu_no)
        {
            return db.GetOBUNO(obu_no);
        }

        public IEnumerable<OPERATOR> GetAllOperators()
        {
            return db.GetOPERATORS();
        }

        public OPERATOR GetOPERATOR(string operNo)
        {
            return db.GetOPERATOR(operNo);
        }

        /// <summary>
        /// 返回操作员权限
        /// </summary>
        /// <param name="operatorNo"></param>
        /// <returns></returns>
        public IEnumerable<Operator_FunctionInfo> GetOperatorFunctions(string operatorNo)
        {
            return db.GetOperatorFunctions(operatorNo);
        }

        public AgentInfo GetAgentInfo(string agentNo)
        {
            return db.GetAgentByNo(agentNo);
        }

        public AgentInfo GetAgentInfo(long PosID)
        {
            return db.GetAgentByPosID(PosID);
        }

        public void Dispose()
        {
            db.Dispose();
        }
    }
}
